package liuh.com.ledlamp.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import liuh.com.ledlamp.R;
import liuh.com.ledlamp.handle.DisTitle;
import liuh.com.ledlamp.util.FileKit;

/**
 * @author :liuh
 * @note
 * @date: 2016-11-30
 */

public class NormalRecyclerViewAdapter extends RecyclerView.Adapter<NormalRecyclerViewAdapter.NormalTextViewHolder> {
    private static final String TAG = "RecyclerAdapter";

    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    //private String[] mTitles;
    private ArrayList<String> mTitles = new ArrayList<>();
    private OnItemClickLitener mOnItemClickLitener;

    public NormalRecyclerViewAdapter(Context context) {
        //mTitles = context.getResources().getStringArray(R.array.titles);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NormalTextViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
    }

    @Override
    public void onBindViewHolder(final NormalTextViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder--"+position);
        holder.mTextView.setTextColor(ContextCompat.getColor(mContext, R.color.color_0176da));
        holder.itemView.setTag("");
        try {
            Map<String, String> dis_title = (Map<String, String>) new FileKit(mContext).readObject("dis_title");
            if(dis_title!=null){
                if(dis_title.containsValue(mTitles.get(position))){
                    Log.d(TAG, "onBindViewHolder---"+position);
                    String wid = DisTitle.getInstance().getKeyByValue(dis_title, mTitles.get(position));
                    holder.mTextView.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent));
                    holder.itemView.setTag(wid);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.mTextView.setText(mTitles.get(position));
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mTitles == null ? 0 : mTitles.size();
    }

    public void addTitles(String[] titles) {
        Collections.addAll(mTitles, titles);
        notifyItemRangeInserted(0, titles.length);
    }

    public boolean addTitle(String title) {
        if(!mTitles.contains(title)){
            if(mTitles.size()>0){
                mTitles.add(1, title);
                notifyItemInserted(1);
            }else{
                mTitles.add(0, title);
                notifyItemInserted(0);
            }
            // 数据保存
            try {
                new FileKit(mContext).saveObject(mTitles,"title_list");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }else{
            return false;
        }
    }

    public boolean updTitle(int index,String title){
        if(!mTitles.contains(title)){
            mTitles.set(index,title);
            notifyItemRangeChanged(index,1);
            // 数据保存
            try {
                new FileKit(mContext).saveObject(mTitles,"title_list");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }else{
            return false;
        }
    }

    public void remove(int position) {
        mTitles.remove(position);
        notifyItemRemoved(position);
        // 数据保存
        try {
            new FileKit(mContext).saveObject(mTitles,"title_list");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<String> getTitles(){
        return mTitles;
    }

    //item 事件监听
    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    // 事件回调
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public static class NormalTextViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.text_view)
        TextView mTextView;

        NormalTextViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
